package com.xiaoyu.shenyou.shenyouv1.controller;

import cn.hutool.json.JSONUtil;
import com.xiaoyu.shenyou.shenyouv1.common.result.Result;
import com.xiaoyu.shenyou.shenyouv1.common.shenyouentity.RootBean;
import com.xiaoyu.shenyou.shenyouv1.common.shenyouentity.accesstoken.AccessToken;
import com.xiaoyu.shenyou.shenyouv1.dto.*;
import com.xiaoyu.shenyou.shenyouv1.entity.AutoConfigEntity;
import com.xiaoyu.shenyou.shenyouv1.entity.AutoLogEntity;
import com.xiaoyu.shenyou.shenyouv1.entity.AutoUserEntity;
import com.xiaoyu.shenyou.shenyouv1.services.AutoPlayService;
import com.xiaoyu.shenyou.shenyouv1.vo.AutoUserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/v1/autoplay")
@Slf4j
public class AutoPlayController {

    @Autowired
    private AutoPlayService autoPlayService;


    /**
     * 发送验证码
     *
     * @param phoneDTO
     * @return
     */
    @PostMapping("/send")
    public Result send(@RequestBody PhoneDTO phoneDTO) {

        RootBean post = autoPlayService.sendMsg(phoneDTO);
        if (post.getCode() == 200) {
            return Result.success(null);
        }
        return Result.error(post.getMsg());
    }

    @PostMapping("/code")
    public Result code(@RequestBody CodeDTO codeDTO) {
        RootBean bean = autoPlayService.submitPhoneAndCode(codeDTO);
        if (bean.getCode() == 200) {
            AccessToken token = JSONUtil.toBean(bean.getData(), AccessToken.class);
            return Result.success(token.getAccess_token());
        }
        return Result.error(bean.getMsg());
    }

    /**
     * 登录挂机
     *
     * @return
     */
    @PostMapping("login")
    public Result login(@RequestBody LoginDTO loginDTO) {
        AutoUserEntity autoUser = autoPlayService.userLogin(loginDTO);
        if (autoUser == null) {
            Result<String> error = Result.error("token错误或过期");
            return error;
        }
        AutoUserVO autoUserVO = new AutoUserVO();
        autoPlayService.insertDefaultAutoConfig(autoUser);
        BeanUtils.copyProperties(autoUser, autoUserVO);
        Result<AutoUserVO> success = Result.success(autoUserVO);
        return success;
    }


    /**
     * 操作挂机
     *
     * @return
     */
    @PostMapping("/auto")
    public Result auto(@RequestBody AutoPlayDTO autoPlayDTO) {
        autoPlayService.updateAuto(autoPlayDTO);
        return Result.success(null);
    }

    /**
     * 用户修改配置表
     *
     * @param autoConfigDTO
     * @return
     */
    @PostMapping("/autoconfig")
    public Result update(@RequestBody AutoConfigDTO autoConfigDTO) {
        autoPlayService.updateAutoConfig(autoConfigDTO);
        return Result.success(null);
    }

    /**
     * 查询用对应的配置
     *
     * @param autoUserDTO
     * @return
     */
    @GetMapping("/autoconfig")
    public Result getConfig(AutoUserDTO autoUserDTO) {
        AutoConfigEntity autoConfig = autoPlayService.getAutoConfigById(autoUserDTO.getUser_id());
        if (autoConfig == null) {
            Result<String> error = Result.error("不存在配置");
            return error;
        }
        Result<AutoConfigEntity> success = Result.success(autoConfig);
        return success;
    }

    /**
     * 查询用户对应的日志
     *
     * @param autoUserDTO
     * @return
     */
    @GetMapping("/autologs")
    public Result getAutoLogs(AutoUserDTO autoUserDTO) {
        List<AutoLogEntity> autoLogs = autoPlayService.getAutoLogsById(autoUserDTO);
        if (autoLogs == null) {
            Result<String> error = Result.error("自动日志不存在！");
            return error;
        }
        Result<List<AutoLogEntity>> success = Result.success(autoLogs);
        return success;
    }

    /**
     * 调房
     *
     * @param changRoomDTO
     * @return
     */
    @PostMapping("/chang")
    public Result changRoom(@RequestBody ChangRoomDTO changRoomDTO) {
        boolean changed = autoPlayService.changRoom(changRoomDTO);
        if (changed) {
            return Result.success(null);
        } else {
            return Result.error("尚未开启挂机");
        }
    }

}
